from dao.call_dao import (insert_call_into_at_class, exist_z_t_j)
from dao.major_dao import (get_major_by_m_id)
from dao.at_class_dao import (get_record, fix_record_class)
from dao.at_major_dao import (get_at_major_by_m_id_and_week_and_day,
                              insert_into_major_call,
                              update_major_call,
                              fix_record_major)
from dao.at_department_dao import (get_at_department_by_f_id_and_week_and_day,
                                   insert_into_department_call,
                                   update_department_call,
                                   fix_record_department)


def save_call(records):
    b = True
    msg = ''
    if len(records) < 1:
        return False, '无成员，无需考勤'
    # 班级考勤
    for a_record in records:
        # 验证是否存在节次考勤
        if len(exist_z_t_j(a_record["class_id"], a_record["week"], a_record["day"], a_record["th"])) == len(records):
            msg = '已存在该节次信息'
            return not b, msg
        sql = """insert into `se_at_class`(`class_id`, `user_id`, `major_id`, 
        `course_id`, `week`, `day`, `th`, `state`, `teacher`) values ('{}', {}, {}, '{}', {}, {}, {}, {}, '{}')""".format(
            a_record["class_id"], a_record["u_id"], a_record["m_id"], a_record["course_id"], a_record["week"],
            a_record["day"], a_record["th"], a_record["state"], a_record["teacher"]
        )
        r = insert_call_into_at_class(sql)
        b &= r
    # 专业考勤
    # 捞一个学生，查看专业id
    if len(records) > 0:
        record = records[0]
        m_id = record["m_id"]
        # 根据专业id找到院系id
        a_major = get_major_by_m_id(m_id)
        f_id = a_major["f_id"]
        # 判断专业是否今天第一次考勤
        week = record["week"]
        day = record["day"]
        at_number = 0
        ttt = filter(lambda x: x["state"] == 0, records)
        for tt in ttt:
            at_number+=1
        # 第一次考勤
        if len(get_at_major_by_m_id_and_week_and_day(m_id, week, day)) == 0:
            sql = """insert into `se_at_major`(`major_id`, `department_id`, `major_at_number`, 
            `major_all_number`, `week`, `day`, `m_rate`) values({}, {}, {}, {}, {}, {}, '{}')""".format(
                m_id, f_id, at_number, len(records), week, day, str(format(100*at_number/len(records), ".2f")) + "%"
            )
            r = insert_into_major_call(sql)
            b &= r
            if not r:
                msg = '出错了'
                return b, msg
        # 不是第一次就更新
        else:
            a_at_major = get_at_major_by_m_id_and_week_and_day(m_id, week, day)[0]
            sql = """UPDATE `se_at_major` SET `major_at_number`=`major_at_number`+{}, 
                        `major_all_number`={} + `major_all_number`, `m_rate`='{}' WHERE `day`={} AND `week`={}
                         AND `major_id`={} AND `department_id`={}""".format(
                at_number, len(records), str(format((100 * (a_at_major["major_at_number"]  + at_number)) / (a_at_major["major_all_number"] + len(records)), ".2f")) + '%', int(day), int(week), m_id, f_id
            )
            # print(sql)
            r = update_major_call(sql)
            b &= r
            if not r:
                msg = '出错了'
                return b, msg
        # 院系考勤
        # 第一次考勤
        if len(get_at_department_by_f_id_and_week_and_day(f_id, week, day)) == 0:

            sql = """insert into `se_at_department`(`department_all_number`, `department_at_number`, `department_id`, 
            `week`, `day`, `f_rate`) values({}, {}, {}, {}, {}, '{}')""".format(
                len(records), at_number, f_id, week, day, str(format(100*at_number/len(records), ".2f")) + '%'
            )
            r = insert_into_department_call(sql)
            b &= r
            if not r:
                msg = '出错了'
                return b, msg
        # 不是第一次就更新
        else:
            a_at_department = get_at_department_by_f_id_and_week_and_day(f_id, week, day)[0]
            sql = """update `se_at_department` set `department_all_number`=`department_all_number`+{}, 
            `department_at_number`=`department_at_number`+{},
                    `f_rate` = '{}' where `week`={} and `day`={} and `department_id`={}""".format(
                len(records), at_number, str(format((100 * (a_at_department["department_at_number"]  + at_number)) / (a_at_department["department_all_number"] + len(records)), ".2f")) + '%', week, day, f_id
            )
            r = update_department_call(sql)
            b &= r
            if not r:
                msg = '出错了'
                return b, msg
    return b, msg


def get_call(z, t, j, course_id, class_id):
    sql = """SELECT * FROM `se_at_class` WHERE `week`={} and `day`={} and `th`={} and 
    `class_id`={} and `course_id`={}""".format(z, t, j, class_id, course_id)
    return get_record(sql)


def fix_call_class(records, week, day, th, class_id):
    for record in records:
        sql = """UPDATE `se_at_class` SET `state`={} WHERE `week`={} AND `day`={} 
        AND `th`={} AND `class_id`='{}' AND `course_id`={} AND `user_id`={}""".format(
            record["state"], week, day, th, class_id, record["course_id"], record["u_id"]
        )
        # print(sql)
        fix_record_class(sql)


def fix_call_major(week, day, m_id, n):
    c = '-' if n < 0 else '+'
    a_at_major = get_at_major_by_m_id_and_week_and_day(m_id, week, day)[0]
    sql = """UPDATE `se_at_major` SET `major_at_number`=`major_at_number` {} {},
     `m_rate`='{}' 
      WHERE `major_id`={} 
    AND `week`={} AND `day`={}""".format(
        c, abs(n), str(format((100 * (a_at_major["major_at_number"] + n)) / a_at_major["major_all_number"],
                              ".2f")) + "%", m_id, week, day
    )
    print(sql)
    fix_record_major(sql)


def fix_call_department(week, day, m_id, n):
    c = '-' if n < 0 else '+'
    a_major = get_major_by_m_id(m_id)
    f_id = a_major["f_id"]
    a_at_department = get_at_department_by_f_id_and_week_and_day(f_id, week, day)[0]
    sql = """UPDATE `se_at_department` SET `department_at_number`=`department_at_number` {} {} ,
     `f_rate`='{}' 
     WHERE `department_id`={} 
    AND `week`={} AND `day`={}""".format(
        c, abs(n), str(format((100 * (a_at_department["department_at_number"] + n)) / a_at_department["department_all_number"], ".2f")) + "%", f_id, week, day
    )
    fix_record_department(sql)
